﻿using System;
using System.Net;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.Services.Protocols;
using System.Diagnostics;

public class WsdlAddress : IService
{
    public string GetData(int value)
    {
        return string.Format("You entered: {0}", value);
    }

    public CompositeType GetDataUsingDataContract(CompositeType composite)
    {
        if (composite == null)
        {
            throw new ArgumentNullException("composite");
        }
        if (composite.BoolValue)
        {
            composite.StringValue += "Suffix";
        }
        return composite;
    }

    //scrape the html code for a given web page for the sweet, sweet nectar of WSDL addresses
    public string[] ReturnWsdlAddresses(string url)
    {
        //grab the desired HTML as a string
        WebClient client = new WebClient();
        string tempString = client.DownloadString(url);
        Regex r;

        //use regular expressions to find any compatible WSDL addresses (.wsdl and ?wsdl)
        r = new Regex(@"(((http://www)|(http://)|(www))[-a-zA-Z0-9@:%_\+.~#?&//=]+)(\.wsdl|\?wsdl)", RegexOptions.IgnoreCase);

        MatchCollection MatchList = r.Matches(tempString);
        string[] tempStringArray = new string[MatchList.Count];

        //pour the WSDL addresses in the required string array
        for (int i = 0; i < MatchList.Count; i++)
        {
            Match m;

            m = MatchList[i];
            tempStringArray[i] = m.Value;
        }

        //send the addresses off to their new home
        return tempStringArray;
    }
}
